package patrol.mobile.com.mobilepatrol.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import butterknife.BindView;
import io.realm.Realm;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.bridge.offen.BridgeOffenCheckListActivity;
import patrol.mobile.com.mobilepatrol.activity.bridge.regular.BridgeRegularCheckListActivity;
import patrol.mobile.com.mobilepatrol.bean.Qiaoliang;
import patrol.mobile.com.mobilepatrol.bean.Qldqjcb;
import patrol.mobile.com.mobilepatrol.bean.Qljcxjcb;
import patrol.mobile.com.mobilepatrol.listener.TimeListener;
import patrol.mobile.com.mobilepatrol.manager.QManager;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;

import static patrol.mobile.com.mobilepatrol.PatrolApp.realm;
import static patrol.mobile.com.mobilepatrol.R.array.check;
import static patrol.mobile.com.mobilepatrol.R.id.tv;

/**
 * Created by apple on 17/9/29.
 */

public class SignActivity extends BaseActivity {
    @BindView(R.id.item1)
    RelativeLayout rl1;

    @BindView(R.id.item2)
    RelativeLayout rl2;

    @BindView(R.id.item3)
    RelativeLayout rl3;
    EditText et2;
    TextView tv4;
    private Spinner spinner;
    @BindView(R.id.bt)
    Button bt;
    //true定期检查，false经常检查
    boolean status = true;
    private String qlcode;
    Qiaoliang qMes;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_sign;
    }

    @Override
    public void initView() {
        super.initView();
        TextView tv1 = rl1.findViewById(tv);
        tv1.setText("签到分类");
        spinner = rl1.findViewById(R.id.spinner);
        Resources res = getResources();
        String[] checks = res.getStringArray(check);
        // 建立Adapter并且绑定数据源
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, checks);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //绑定 Adapter到控件
        spinner .setAdapter(adapter);

        TextView tv2 = rl2.findViewById(tv);
        tv2.setText("签到人员");
        et2 = rl2.findViewById(R.id.et);
        et2.setText(UserManager.getUser((String) SPUtils.get(Constants.current_id, "")).ryname);
//        et2.setSelection(Constants.current_ryname.length());

        TextView tv3 = rl3.findViewById(tv);
        tv3.setText("签到日期");
        tv4 = rl3.findViewById(R.id.tv1);
        tv4.setText(CommonUtil.getCurrentDate());
    }

    @Override
    public void initData() {
        super.initData();
        qlcode = getIntent().getStringExtra("qlcode");
        qMes = QManager.getQMes(qlcode);
    }

    @Override
    public void setListener() {
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                    if (pos==0){
                        status = true;

                    }else{
                        status = false;
                    }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                final String name = et2.getText().toString().trim();
                final String time = tv4.getText().toString().trim();
                if (status){
                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                                    Qldqjcb bridgeDQ = realm.createObject(Qldqjcb.class);
                                    bridgeDQ.qldqjcid = CommonUtil.getUUID();
                                    bridgeDQ.roadcode = qMes.roadcode;
                                    bridgeDQ.roadname = qMes.roadname;
                                    bridgeDQ.roadpos = qMes.pos;
                                    bridgeDQ.qlcode = qMes.qlcode;
                                    bridgeDQ.qlname = qMes.name;
                                    bridgeDQ.xctdm = "";
                                    bridgeDQ.qc = qMes.qlcd;
                                    bridgeDQ.zkjg = "";
                                    bridgeDQ.zdkj = 0;
                                    bridgeDQ.gydw = qMes.gydwmc;
                                    bridgeDQ.jcny = qMes.jcsj;
                                    bridgeDQ.scdzxrq = CommonUtil.getCurrentDate();
                                    bridgeDQ.scjcrq = CommonUtil.getCurrentDate();
                                    bridgeDQ.bcjcrq = time;
                                    bridgeDQ.xcjcsj = CommonUtil.getCurrentDate();
                                    bridgeDQ.qh = "";
                                    bridgeDQ.ztzkpddj = "";
                                    bridgeDQ.qlqjzkpf = 0;
                                    bridgeDQ.byxxzkpf = 0;
                                    bridgeDQ.jcxyhjy = "";
                                    bridgeDQ.jlr = name;
                                    bridgeDQ.fzr = "";
                                    bridgeDQ.qssm = "";

                                    bridgeDQ.weather_id = 0;
                                    bridgeDQ.fen_clean_id = 0;
                                    bridgeDQ.fen_ser_id = 0;
                                    bridgeDQ.fen_total_id = 0;
                                    bridgeDQ.isUp = false;
                                    qMes.dqjc.add(bridgeDQ);
                                    qMes.isUp = false;
                                }
                    });
                    Intent intent = new Intent(SignActivity.this,BridgeRegularCheckListActivity.class);
                    intent.putExtra("qlcode",qlcode);
                    startActivity(intent);
                    CommonUtil.openNewActivityAnim(SignActivity.this,true);
                }else{
                    //经常检查，已经qlcode，获取桥梁
                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            Qljcxjcb jc = realm.createObject(Qljcxjcb.class);
                            jc.id = CommonUtil.getUUID();
                            jc.roadcode = qMes.roadcode;
                            jc.roadname = qMes.roadname;
                            jc.roadpos = qMes.pos;
                            jc.qlcode = qMes.qlcode;
                            jc.qlname = qMes.name;
                            jc.gydw = qMes.gydwmc;
                            jc.jlr = name;
                            jc.fzr = "";
                            jc.jcrq = time;
                            jc.isUp = false;
                            qMes.jcxjc.add(jc);
                            qMes.isUp = false;
                        }

                    });

                    Intent intent = new Intent(SignActivity.this,BridgeOffenCheckListActivity.class);
                    intent.putExtra("qlcode",qlcode);
                    startActivity(intent);
                    CommonUtil.openNewActivityAnim(SignActivity.this,true);

                }



            }
        });
        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(SignActivity.this,new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        tv4.setText(date);
                    }
                });
            }
        });

    }
    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(SignActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("签到");
    }
}
